package backstage.staff.service.impl;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;

import backstage.staff.action.StaffAction;
import backstage.staff.dao.StaffDao;
import backstage.staff.pojo.Staff;
import backstage.staff.service.StaffService;
import backstage.sys.util.MD5;

public class StaffServiceImpl implements StaffService {
	
	private static Logger logger = Logger.getLogger(StaffAction.class);
	
	private StaffDao staffDao;

	public String verifi(Staff po,HttpSession session) {
		String request = null;
		byte[] pwd = po.getSysPwd().getBytes();
		Staff staff = this.staffDao.getStaff(po.getSysName());
		if(null != staff && null != staff.getSysPwd()){
			if(!staff.getSysPwd().equals(MD5.getMD5(MD5.getMD5(pwd).getBytes()))){
				request = "密码错误!";
				logger.error("密码错误!用户:" + staff.getSysName());
			}else{
				logger.info("用户登录:" + staff.getSysName());
				session.setAttribute("staff", staff);
			}
		}else{
			request = "用户名不存在或密码为空";
		}
		return request;
	}

	public Staff addStaff(Staff po) {
		Integer i = staffDao.seva(po);
		po.setId(i);
		return po;
	}

	public void updateStaff(Staff po) {
		po.setSysPwd(MD5.getMD5(MD5.getMD5(po.getSysPwd().getBytes()).getBytes()));
		staffDao.update(po);
	}

	public List<Staff> getStaffList(Staff po) {
		StringBuffer hql = new StringBuffer();
		hql.append("from StaffPO ");
		int i = 0;
		if( null != po.getId() && !"".equals(po.getId())){
			hql.append("where id=").append(po.getId());
			++i;
		}
		if( null != po.getSysName() && !"".equals(po.getSysName())){
			if( i != 0){
				hql.append(" and sysName='").append(po.getSysName()).append("'");
			}else{
				hql.append("where sysName='").append(po.getSysName()).append("'");
			}
		}
		
		if( null != po.getIdNumber() && !"".equals(po.getIdNumber())){
			if( i != 0){
				hql.append(" and idNumber='").append(po.getIdNumber()).append("'");
			}else{
				hql.append("where idNumber='").append(po.getIdNumber()).append("'");
			}
		}
		if( null != po.getPhone() && !"".equals(po.getPhone())){
			if( i != 0){
				hql.append(" and phone='").append(po.getPhone()).append("'");
			}else{
				hql.append("where phone='").append(po.getPhone()).append("'");
			}
		}
		if( null != po.getMailbox() && !"".equals(po.getMailbox())){
			if( i != 0){
				hql.append(" and mailbox='").append(po.getMailbox()).append("'");
			}else{
				hql.append("where mailbox='").append(po.getMailbox()).append("'");
			}
		}
		if( null != po.getAddress() && !"".equals(po.getAddress())){
			if( i != 0){
				hql.append(" and address like %'").append(po.getAddress()).append("'");
			}else{
				hql.append("where address like %'").append(po.getAddress()).append("'");
			}
		}
		if( -1 != po.getStatus()){
			if( i != 0){
				hql.append(" and status=").append(po.getStatus());
			}else{
				hql.append("where status=").append(po.getStatus());
			}
		}
		/*
		if( null != po.getEntryTime() && !"".equals(po.getEntryTime())){
			if( i != 0){
				hql.append(" and entryTime=").append(po.getEntryTime());
			}else{
				hql.append("where entryTime=").append(po.getEntryTime());
			}
		}
		if( null != po.getDepartureTime() && !"".equals(po.getDepartureTime())){
			if( i != 0){
				hql.append(" and departureTime=").append(po.getDepartureTime());
			}else{
				hql.append("where departureTime=").append(po.getDepartureTime());
			}
		}
		*/
		if( null != po.getPost() && !"".equals(po.getPost())){
			if( i != 0){
				hql.append(" and post='").append(po.getPost()).append("'");
			}else{
				hql.append("where post='").append(po.getPost()).append("'");
			}
		}
		if( -1 != po.getEmployees()){
			if( i != 0){
				hql.append(" and employees=").append(po.getEmployees());
			}else{
				hql.append("where employees=").append(po.getEmployees());
			}
		}
		staffDao.getStaffList(hql.toString());
		return null;
	}
	
	public StaffDao getStaffDao() {
		return staffDao;
	}

	public void setStaffDao(StaffDao staffDao) {
		this.staffDao = staffDao;
	}

}
